from django.shortcuts import render, get_object_or_404, redirect
from django.urls import reverse_lazy
from django.views.generic import ListView, DetailView, CreateView, UpdateView
from django.views import View
from .models import CameraEntry, CameraCategory, CameraAccessory, AccessoryCategory
from .forms import CameraEntryForm, CameraAccessoryForm

# 相机列表视图
class CameraEntryListView(ListView):
    model = CameraEntry
    template_name = 'encyclopedia/camera_list.html'
    context_object_name = 'cameras'
    paginate_by = 10
    
    def get_queryset(self):
        queryset = super().get_queryset().order_by('-created_at')
        # 支持按分类过滤
        category_id = self.request.GET.get('category')
        if category_id:
            queryset = queryset.filter(category_id=category_id)
        # 支持搜索
        search = self.request.GET.get('search')
        if search:
            queryset = queryset.filter(title__icontains=search) | queryset.filter(content__icontains=search)
        return queryset
    
    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['categories'] = CameraCategory.objects.all()
        return context

# 相机详情视图
class CameraEntryDetailView(DetailView):
    model = CameraEntry
    template_name = 'encyclopedia/camera_detail.html'
    context_object_name = 'camera'
    
    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        # 添加兼容的配件信息
        context['compatible_accessories'] = self.object.compatible_accessories.all()
        return context

# 相机创建视图
class CameraEntryCreateView(CreateView):
    model = CameraEntry
    form_class = CameraEntryForm
    template_name = 'encyclopedia/camera_form.html'
    success_url = reverse_lazy('encyclopedia:camera_list')

# 相机编辑视图
class CameraEntryUpdateView(UpdateView):
    model = CameraEntry
    form_class = CameraEntryForm
    template_name = 'encyclopedia/camera_form.html'
    success_url = reverse_lazy('encyclopedia:camera_list')

# 分类列表视图
class CategoryListView(ListView):
    model = CameraCategory
    template_name = 'encyclopedia/category_list.html'
    context_object_name = 'categories'

# 分类详情视图 - 显示某一分类下的所有相机
class CategoryDetailView(DetailView):
    model = CameraCategory
    template_name = 'encyclopedia/category_detail.html'
    context_object_name = 'category'
    
    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['cameras'] = self.object.cameras.order_by('-created_at')
        return context

# 删除相机视图
def camera_delete(request, pk):
    camera = get_object_or_404(CameraEntry, pk=pk)
    if request.method == 'POST':
        camera.delete()
        return redirect('encyclopedia:camera_list')
    return render(request, 'encyclopedia/camera_confirm_delete.html', {'camera': camera})

# 配件列表视图
class AccessoryListView(ListView):
    model = CameraAccessory
    template_name = 'encyclopedia/accessory_list.html'
    context_object_name = 'accessories'
    paginate_by = 10
    
    def get_queryset(self):
        queryset = super().get_queryset().order_by('-created_at')
        # 支持按分类过滤
        category_id = self.request.GET.get('category')
        if category_id:
            queryset = queryset.filter(category_id=category_id)
        # 支持搜索
        search = self.request.GET.get('search')
        if search:
            queryset = queryset.filter(title__icontains=search) | queryset.filter(content__icontains=search)
        return queryset
    
    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['categories'] = AccessoryCategory.objects.all()
        return context

# 配件详情视图
class AccessoryDetailView(DetailView):
    model = CameraAccessory
    template_name = 'encyclopedia/accessory_detail.html'
    context_object_name = 'accessory'

# 配件创建视图
class AccessoryCreateView(CreateView):
    model = CameraAccessory
    form_class = CameraAccessoryForm
    template_name = 'encyclopedia/accessory_form.html'
    success_url = reverse_lazy('encyclopedia:accessory_list')

# 配件编辑视图
class AccessoryUpdateView(UpdateView):
    model = CameraAccessory
    form_class = CameraAccessoryForm
    template_name = 'encyclopedia/accessory_form.html'
    success_url = reverse_lazy('encyclopedia:accessory_list')

# 配件分类列表视图
class AccessoryCategoryListView(ListView):
    model = AccessoryCategory
    template_name = 'encyclopedia/accessory_category_list.html'
    context_object_name = 'categories'

# 配件分类详情视图 - 显示某一分类下的所有配件
class AccessoryCategoryDetailView(DetailView):
    model = AccessoryCategory
    template_name = 'encyclopedia/accessory_category_detail.html'
    context_object_name = 'category'
    
    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['accessories'] = self.object.accessories.order_by('-created_at')
        return context

# 删除配件视图
def accessory_delete(request, pk):
    accessory = get_object_or_404(CameraAccessory, pk=pk)
    if request.method == 'POST':
        accessory.delete()
        return redirect('encyclopedia:accessory_list')
    return render(request, 'encyclopedia/accessory_confirm_delete.html', {'accessory': accessory})

# 关于我们页面视图
class AboutView(View):
    def get(self, request):
        context = {
            'camera_count': CameraEntry.objects.count(),
            'accessory_count': CameraAccessory.objects.count(),
            'category_count': CameraCategory.objects.count(),
            'accessory_category_count': AccessoryCategory.objects.count()
        }
        return render(request, 'encyclopedia/about.html', context)
